<template>
  <div>
    <!-- 表格 -->
    <div class="div_box_table">
    <el-table :data="reslist" style="width: 100%" id="table" border>
      <el-table-column type="selection" width="55"> </el-table-column>
      <el-table-column prop="uid" label="ID" width="200"> </el-table-column>
      <!-- 双击变大 -->
      <el-table-column label="图片">
        <template slot-scope="scope">
          <!-- <img :src="scope.row.avatar" width="80px" /> -->
          <el-image
            style="width: 80px;"
            :src="scope.row.avatar"
            :preview-src-list="srcList"
            @click="srcList.push(scope.row.avatar)"
          >
          </el-image>
        </template>
      </el-table-column>

      <el-table-column prop="nickname" label="姓名" width="120">
      </el-table-column>
      <el-table-column prop="adress" label="地址" width="120">
      </el-table-column>
      <el-table-column prop="mobile" label="手机号码" width="120">
      </el-table-column>
      <el-table-column prop="created_at" label="创建时间" width="250">
        <template slot-scope="scope">
          {{ scope.row.created_at | filtime }}
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" width="150">
        <template slot-scope="scope">
          <el-button @click="add_edit(scope.row)" size="small">编辑</el-button>
          <el-button size="small" @click="remove">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    </div>
    <!-- 分页器 -->
    <el-pagination
      background
      layout="total, sizes, prev, pager, next, jumper"
      :total="list.length"
      :page-size="size"
      :page-sizes="[2, 3, 4, 5]"
      :current-page="page"
      @current-change="change"
      @size-change="changes"
    >
    </el-pagination>

    <!-- 编辑的弹出框 -->
    <el-dialog title="收货地址" :visible.sync="dialogFormVisible">
      <el-form :model="form">
        <el-form-item label="用户名">
          <el-input v-model="form.nickname"></el-input>
        </el-form-item>
        <el-form-item label="手机号码">
          <el-input v-model="form.mobile"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="yes">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      list: [],
      srcList:[], //双击变大
      size: 5,
      page: 1,
      dialogFormVisible: false,
      form: {
        nickname: "",
        mobile: "",
      },
    };
  },

  mounted() {
    this.$axios.get("/list.json").then((res) => {
      console.log(res);
      this.list = res.data;
    });
  },
  //计算属性
  computed: {
    reslist() {
      return this.list.slice(
        this.size * (this.page - 1),
        this.size * this.page
      );
    },
  },
  methods: {
    //分页器
    change(val) {
      this.page = val;
    },
    changes(a) {
      this.size = a;
    },

    //删除
    remove(index) {
      this.list.splice(index, 1);
    },

    //编辑按钮
    add_edit(val) {
      this.dialogFormVisible = true;

      this.form.nickname = val.nickname;
      this.form.mobile = val.mobile;
      this.form.uid = val.uid;
    },

    //编辑里的确定按钮
    yes() {
      this.dialogFormVisible = false;
      this.list.forEach((item) => {
        if (item.uid == this.form.uid) {
          item.uid = this.form.uid;
          item.nickname = this.form.nickname;
          item.mobile = this.form.mobile;
        }
      });
    },
  },

  //   时间过滤器
  filters: {
    filtime(val) {
      var date = new Date(val * 1000);
      var week = [
        "星期日",
        "星期一",
        "星期二",
        "星期三",
        "星期四",
        "星期五",
        "星期六",
      ];
      var year = date.getFullYear();
      var month = date.getMonth() + 1;
      var datee = date.getDate();
      var day = date.getDay();
      var time = year + "年" + month + "月" + datee + "日  " + week[day];
      function getTimerr() {
        var time = new Date();
        var hour = time.getHours();
        hour = hour < 10 ? "0" + hour : hour;
        var minute = time.getMinutes();
        minute = minute < 10 ? "0" + minute : minute;
        var second = time.getSeconds();
        second = second < 10 ? "0" + second : second;
        return hour + ":" + minute + ":" + second;
      }
      return time + " " + getTimerr();
    },
  },
};
</script>

<style lang="scss">
#table{
    text-align: center;
}
.div_box_table{
    width: 100%;
    height: 80vh;
    overflow:scroll;
}

</style>